// 定位模块
import store from "@/store";
import {
  isWeChatBrowser
} from "./utils.js";
import {
  isOpenPermission
} from "./permission.js";

// #ifdef H5
// 微信公众号获取定位信息
export function wxPublicGetLocation(successCallback = () => {}, errCallback = () => {}) {
  let result = isWeChatBrowser();
  if (result) {
    //配置校验成功后执行
    if (jWeixin) {
      jWeixin.ready(() => {
        jWeixin.getLocation({
          type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
          success: (res) => {
            successCallback(res);
          },
          fail: (err) => {
            errCallback(err);
          }
        });
      });
    } else {
      uni.showToast({
        icon: 'none',
        title: "微信SDK未配置,请检查"
      });
    }
  } else {
    uni.showToast({
      icon: 'none',
      title: "请在微信客户端浏览器调用"
    });
  }
}
// #endif

// 获取定位信息
export function getLocation(successCallback = () => {}, errCallback = () => {}) {
  // 除APP平台以外
  // #ifndef APP-PLUS
  uni.getLocation({
    type: 'gcj02',
    success: locationRes => {
      store.commit("setState", {
        name: "location",
        data: locationRes,
      });
      successCallback(locationRes);
    },
    fail: (error) => {
      console.log("位置信息错误", error);
      errCallback("位置信息获取失败");
    }
  });
  // #endif

  // #ifdef APP-PLUS
  // APP平台
  isOpenPermission("location", (result) => {
    if (result == 1) {
      uni.getLocation({
        type: 'gcj02',
        success: locationRes => {
          store.commit("setState", {
            name: "location",
            data: locationRes,
          });
          successCallback(locationRes);
        },
        fail: (error) => {
          console.log("位置信息错误", error);
          errCallback("位置信息获取失败");
        }
      });
    }
  });
  // #endif
};
